package com.hds.modules.app.controller;


import com.hds.common.upload.UploadUtils;
import com.hds.common.utils.R;
import com.hds.modules.sys.controller.AbstractController;
import com.hds.modules.sys.entity.config.UploadFileConfig;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * APP upload
 *
 * @date 2018-07-23 15:31
 */
@RestController
@RequestMapping("/app/file")
@Api("文件上传")
public class AppUploadController extends AbstractController{

    @Autowired
    UploadFileConfig uploadFileConfig;



    /**
     * 通过url请求返回图像的字节流
     * imageName:过滤掉特殊字符"."
     */
    @RequestMapping(value={"/{imageName:.+}"})
    public void getIcon(HttpServletRequest request,
                        HttpServletResponse response,
                        @PathVariable("imageName") String imageName,
                        @RequestParam(value="w",required=false) Integer width,
                        @RequestParam(value="h",required=false) Integer height) throws IOException {

        UploadUtils.getIcon(request, response, uploadFileConfig, imageName, width, height);

    }




    /**
     * 文件上传具体实现方法（单文件上传）
     * @param file
     * @param thumbWidth 图片缩放宽度
     * @param thumbHeight 图片缩放高度
     * @desc 如果thumbWidth,thumbHeight都为空的话，只上传原图。
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public R upload(HttpServletRequest request,
                    @RequestParam(value="w",required=false) Integer thumbWidth,
                    @RequestParam(value="h",required=false) Integer thumbHeight,
                    @RequestParam("file") MultipartFile file) {

       return UploadUtils.upload(request, thumbWidth, thumbHeight, uploadFileConfig, file);

    }



}
